<?php
/**
 * This is the model class for table "user_contact".
 *
 * The followings are the available columns in table 'user_contact':
 * @property string $uid
 * @property string $email
 * @property string $mobile
 * @property string $phone
 * @property string $qq
 * @property string $addtime
 * @property string $addip
 * @property string $updatetime
 * @property string $updateip
 */

class UserContactModel extends Model
{
    public $phoneHead;
    public $phoneBody;
    public $phoneFoot;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UserContact the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_contact';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
		    array('mobile', 'required', 'message' => '您必须输入手机号码。'),
		    array('email', 'required', 'message' => '您必须输入邮箱地址。'),

			array('mobile', 'length', 'max' => 11),
			array('email', 'length', 'max' => 64),
			array('phone', 'length', 'max' => 32),
			array('qq', 'length', 'max'=> 16),

	        array('email', 'email', 'message' => '您输入的邮箱地址格式不正确。'),
	        array('mobile', 'match', 'pattern' => Yii::app()->params['regex']['mobile'], 'message' => '您输入的手机号码不正确。'),
	        array('qq', 'match', 'pattern' => Yii::app()->params['regex']['qq'], 'message' => '您输入的QQ号码不正确。'),

		    array('phoneHead, phoneBody, phoneFoot, phone', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'email' => Yii::t('app', 'email'),
			'mobile' => Yii::t('app', 'mobile'),
		    'phoneHead' => Yii::t('app', 'phone head'),
	        'phoneBody' => Yii::t('app', 'phone body'),
	        'phoneFoot' => Yii::t('app', 'phone foot'),
			'phone' => Yii::t('app', 'phone'),
			'qq' => 'QQ',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('uid',$this->uid,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('qq',$this->qq,true);
		$criteria->compare('addtime',$this->addtime,true);
		$criteria->compare('addip',$this->addip,true);
		$criteria->compare('updatetime',$this->updatetime,true);
		$criteria->compare('updateip',$this->updateip,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * (non-PHPdoc)
	 * @see CModel::beforeValidate()
	 */
	protected function beforeValidate()
	{
	    if (parent::beforeValidate())
	    {
	        if ($this->phone && !preg_match(Yii::app()->params['regex']['phone'], $this->phone))
	        {
	            if (!$this->phoneHead || !preg_match(Yii::app()->params['regex']['phoneHead'], $this->phoneHead))
	            {
	                $this->addError('phoneHead', '');
	            }

	            if (!$this->phoneBody || !preg_match(Yii::app()->params['regex']['phoneBody'], $this->phoneBody))
	            {
	                $this->addError('phoneBody', '');
	            }

	            if ($this->phoneFoot && !preg_match(Yii::app()->params['regex']['phoneFoot'], $this->phoneFoot))
	            {
	                $this->addError('phoneFoot', '');
	            }

	            $this->addError('phone', '您输入的固定电话不正确。');
	        }

	        return TRUE;
	    }

	    return FALSE;
	}
}